package com.scalosoft.action.download;
	 
	import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.validation.SkipValidation;

import com.opensymphony.xwork2.ActionSupport;
	
	@SuppressWarnings("serial")

	@Namespace(value="/download")
	@Result( location = "/download/Download.jsp")
	public class Download extends ActionSupport{
	 
		private InputStream fileInputStream;
		
		private String filename = "test.pdf";
		private String type = "application/pdf";
		
		public String getType() {
			return type;
		}

		public void setType(String type) {
			this.type = type;
		}

		public String getFilename() {
			return filename;
		}

		public void setFilename(String filename) {
			this.filename = filename;
		}

		public InputStream getFileInputStream() {
			return fileInputStream;
		}
		
		@Action(value = "Download",  results = {
				@Result(name = "error", location = "/download/Error.jsp"),
				@Result(name = "success",type="stream",params = {
		                 "contentType","${type}",
		                 "inputName","fileInputStream",
		                 "bufferSize","1024",
		                 "contentDisposition","attachment; filename=${filename}"
		         })
		})
		public String execute() throws Exception {
			ServletContext context = ServletActionContext.getServletContext();
			String filePath = context.getRealPath("/download");
		    fileInputStream = new FileInputStream(new File(filePath,filename));
		    return SUCCESS;
		}
		
		@SkipValidation
		@Action(value="DownloadPage")
		public String init() {
			return SUCCESS;
		}
	}